#     TextLocutor
#     Copyright (C) 2009  Jose Ignacio Naranjo Hernandez (joseignacio.naranjo@gmail.com)

#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU Affero General Public License as
#     published by the Free Software Foundation, either version 3 of the
#     License, or (at your option) any later version.

#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU Affero General Public License for more details.

#     You should have received a copy of the GNU Affero General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.

# python
import logging
import os

# appengine
from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

# textlocutor
import core_settings
import core
from core import views
from core.models import CoreUser


class MainPage(core.BaseRequestHandler):
    def get(self, *args):
        user = users.get_current_user()
        if not user:
            template_values = {
                'base_url': self.request.application_url,
                }
            self.generate(template_values, 'register.html')
        else:
            info = self.init_fixtures(user)
            coreuser = CoreUser.all().filter('user =', users.get_current_user()).get()
            template_values = {
                'user': coreuser,
                'base_url': self.request.host_url,
                'ext_list': core_settings.EXTRA_APPS,
                'logout_url': users.CreateLogoutURL(self.request.uri),
                }
            if info:
                template_values.update(info)
            self.generate(template_values, 'index.html')


class FaqPage(core.BaseRequestHandler):
    def get(self, *args):
        self.response.out.write(template.render(
                os.path.join(core_settings.TEMPLATE_DIR, 'faq.html'), 
                None))

class LicensePage(core.BaseRequestHandler):
    def get(self, *args):
        self.response.out.write(template.render(
                os.path.join(core_settings.TEMPLATE_DIR, 'license.html'), 
                None))


application = webapp.WSGIApplication(
    [('/core/register/$', views.CoreUserRegister),
     ('/core/delete/$', views.CoreUserDelete),
     ('/core/reset_token/$', views.CoreUserResetToken),
     ('/core/user/$', views.CoreUserPage),
     ('/core/$', MainPage),
     ('/faq/$', FaqPage),
     ('/license/$', LicensePage),
     ('/.*$', MainPage)
     ],
    debug=True)

def main():
    logging.getLogger().setLevel(logging.DEBUG)
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
    
